【问题标题】:HQL query help to return latest date from mssql dbHQL 查询帮助从 mssql db 返回最新日期
【发布时间】:2013-11-23 06:44:16
【问题描述】:

我有这个 HQL 查询,它返回表中的所有产品。但我只想获得最新价格日期的产品,而不是旧的产品。我该怎么做。

String select = "SELECT p FROM  ProductPricing p where p.productId.supplierId.id=:id"; // nested condition based on the FK productpricing -> product -> supplier

    Query query = hibernateSession.createQuery(select);
    query.setParameter("id", Integer.parseInt(dsRequest.getFieldValue("supplier").toString()));

    List<ProductPricing> models = query.list();

    for (int i = 0; i < models.size(); i++)
    {
        if (models.get(i).getProductId() != null)
        {
            models.get(i).getProductId().getProductCode();
            models.get(i).getProductId().getBrandName();
            models.get(i).getPurchasePrice();
            System.out.println(models.get(i).getProductId().getBrandName());
        }

    }

我的 ProductPrice 表如下所示。现在我的这个 HQL 查询返回所有记录。但我只希望它返回价格日期最新的记录,即那些 productID 的 id 2 和 4

id priceDate                   purchasePrice salesPrice consumerPrice productID description 

1  2012-11-15 00:00:00         1000          1200       1500          1         qwqw        
2  2013-11-16 00:00:00         1600          1800       2100          1         new price   
3  2012-10-15 00:00:00         1600          1500       1600          2         erere   
4  2013-11-02 00:00:00         1600          1900       3400          2         new price   

【问题讨论】:

  • 您是否为最新价格维护单独的列
  • 什么意思??我的 ProductPricing 表看起来像这个 productPricing (id BIGINT NOT NULL IDENTITY, priceDate DATETIME, purchasePrice FLOAT(53), salesPrice FLOAT(53), consumerPrice FLOAT(53), productID BIGINT,描述 NVARCHAR(255)
  • 请帮忙,我将不胜感激
  • 我有 SQL 就足够了。稍后我会给你 HQL

标签: hibernate hql


【解决方案1】:

你可以这样试试:

String select = "SELECT p FROM  ProductPricing p where p.productId.supplierId.id=:id and p.priceDate = NOW()"; 
Query query = hibernateSession.createQuery(select);
query.setParameter("id", Integer.parseInt(dsRequest.getFieldValue("supplier").toString()));

List<ProductPricing> models = query.list(); 

【讨论】:

  • 请尝试使用 Date = NOW(); 执行示例查询你不会得到任何结果
【解决方案2】:

这是最近 30 天的退货价格

String select = "SELECT p.salesPrice FROM  ProductPricing p where p.productId.supplierId.id=:id ORDER BY p.priceDate desc"; 
Query query = hibernateSession.createQuery(select);
query.setParameter("id", Integer.parseInt(dsRequest.getFieldValue("supplier").toString()));

List<ProductPricing> models = query.list(); 

在您的业务逻辑中过滤结果,我认为您的数据库就是这样设计的(一对多关系)

这是 SQL:

CREATE TABLE `testspring`.`pricing`(     `id` INT NOT NULL AUTO_INCREMENT ,     `priceDate` DATETIME ,     `purchasePrice` INT ,     `salesPrice` INT ,     `consumerPrice` INT ,     `productID` INT ,     `description` VARCHAR(255) ,     PRIMARY KEY (`id`)  );

INSERT INTO `testspring`.`pricing`(`id`,`priceDate`,`purchasePrice`,`salesPrice`,`consumerPrice`,`productID`,`description`) VALUES ( NULL,'2012-11-15 00:00:00','1000','1200','1500','1','qwqw');
INSERT INTO `testspring`.`pricing`(`id`,`priceDate`,`purchasePrice`,`salesPrice`,`consumerPrice`,`productID`,`description`) VALUES ( NULL,'2013-11-16 00:00:00',1500,1600,1500,1,'new price');

INSERT INTO `testspring`.`pricing`(`id`,`priceDate`,`purchasePrice`,`salesPrice`,`consumerPrice`,`productID`,`description`) VALUES ( NULL,'2012-10-15 00:00:00','1600','1500','1600','2','qwqw');
INSERT INTO `testspring`.`pricing`(`id`,`priceDate`,`purchasePrice`,`salesPrice`,`consumerPrice`,`productID`,`description`) VALUES ( NULL,'2013-11-02 00:00:00','1600','1900','3400','2','new price');

SELECT p.salesPrice FROM  pricing p INNER JOIN pricing pp ON p.productID = pp.productID AND p.priceDate>pp.priceDate 
WHERE p.description<>pp.description

【讨论】:

  • 您的 priceDate 列中有哪些数据
  • 日期例如在 priceDate col 有这样的数据 2012-11-15 00:00:00
  • 感谢您的帮助,但您似乎没有理解我的要求。我的表中有一个 col,用于存储产品新价格的日期。我需要的是在我的 HQL 中过滤并获取产品的最新价格
  • @ZAJ 我可以理解一点,不比较两个日期或两个价格,如何获得最新的,或者您只需在查询中调用 salesPrice
  • 请看我的原帖,我已经添加了完整的代码。问题是我需要将 productPricing 对象发送给客户端。我需要过滤掉旧日期
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
  • 2018-04-06
  • 2022-07-04
  • 1970-01-01
相关资源
最近更新 更多