Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6971325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:49:31+00:00 2026-05-27T16:49:31+00:00

This is a simple sort of crosstab query. I am having difficulty transferring my

  • 0

This is a simple sort of crosstab query. I am having difficulty transferring my SQL knowledge to Linq and because it is hard to test (I have not gotten linqpad running properly for WP7 development) I can’t really “play” easily.

CREATE TABLE Store
(
    StoreID INT NOT NULL,
    StoreName VARCHAR(10) NOT NULL
)

INSERT INTO Store (StoreID,StoreName) VALUES (1,'Store A')
INSERT INTO Store (StoreID,StoreName) VALUES (2,'Store B')

CREATE TABLE ProductType
(
    ProductTypeID INT NOT NULL,
    ProductTypeName VARCHAR(20) NOT NULL
)

INSERT INTO ProductType (ProductTypeID,ProductTypeName) VALUES (1,'Clothing')
INSERT INTO ProductType (ProductTypeID,ProductTypeName) VALUES (2,'Food')

CREATE TABLE Product
(
    ProductID INT NOT NULL,
    ProductTypeID INT NOT NULL,
    ProductName VARCHAR(20) NOT NULL
)

INSERT INTO Product (ProductID,ProductTypeID,ProductName) VALUES (1,1,'Hat')
INSERT INTO Product (ProductID,ProductTypeID,ProductName) VALUES (2,1,'Shirt')
INSERT INTO Product (ProductID,ProductTypeID,ProductName) VALUES (3,1,'Pant')
INSERT INTO Product (ProductID,ProductTypeID,ProductName) VALUES (4,2,'Orange')
INSERT INTO Product (ProductID,ProductTypeID,ProductName) VALUES (5,2,'Apple')


CREATE TABLE Purchase
(
    PurchaseID INT NOT NULL,
    ProductID INT NOT NULL,
    StoreID INT NOT NULL,
    Quantity INT NULL,
    Amount INT NULL
)

INSERT INTO Purchase (PurchaseID,ProductID,StoreID,Quantity,Amount) VALUES (1,1,1,5,10)
INSERT INTO Purchase (PurchaseID,ProductID,StoreID,Quantity,Amount) VALUES (2,2,1,4,12)
INSERT INTO Purchase (PurchaseID,ProductID,StoreID,Quantity,Amount) VALUES (3,3,1,1,10)
INSERT INTO Purchase (PurchaseID,ProductID,StoreID,Quantity,Amount) VALUES (4,4,1,3,16)
INSERT INTO Purchase (PurchaseID,ProductID,StoreID,Quantity,Amount) VALUES (5,5,1,7,12)
INSERT INTO Purchase (PurchaseID,ProductID,StoreID,Quantity,Amount) VALUES (6,1,2,10,22)
INSERT INTO Purchase (PurchaseID,ProductID,StoreID,Quantity,Amount) VALUES (7,2,2,8,26)


SELECT s.StoreName
    ,SUM(CASE
        WHEN t.ProductTypeID=1 THEN Amount
        ELSE 0
    END ) as ClothingAmount
    ,SUM(CASE
        WHEN t.ProductTypeID=2 THEN Amount
        ELSE 0
    END )AS FoodQuantity
    ,SUM(CASE
        WHEN t.ProductTypeID=1 THEN Quantity
        ELSE 0
    END ) as ClothingAmount
    ,SUM(CASE
        WHEN t.ProductTypeID=2 THEN Quantity
        ELSE 0
    END )AS FoodQuantity
FROM Purchase u
INNER JOIN Product r
ON u.ProductID=r.ProductID
INNER JOIN ProductType t
ON r.ProductTypeID=t.ProductTypeID
INNER JOIN Store s
ON s.StoreID=u.StoreID
GROUP BY s.StoreName
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:49:32+00:00Added an answer on May 27, 2026 at 4:49 pm

    The following is a 1:1 translation of your SQL query statement in LINQ:

    from u in Purchases
    join r in Products on u.ProductID equals r.ProductID
    join t in ProductTypes on r.ProductTypeID equals t.ProductTypeID
    join s in Stores on u.StoreID equals s.StoreID
    group new {Purchase = u, Product = r, ProductType = t, Store = s} by s.StoreName into grouped
    select new {
        StoreName = grouped.Key,
        ClothingAmount = grouped.Sum(entry => entry.ProductType.ProductTypeID == 1 ? entry.Purchase.Amount : 0),
        FoodAmount = grouped.Sum(entry => entry.ProductType.ProductTypeID == 2 ? entry.Purchase.Amount : 0),
        ClothingQuantity = grouped.Sum(entry => entry.ProductType.ProductTypeID == 1 ? entry.Purchase.Quantity : 0),
        FoodQuantity = grouped.Sum(entry => entry.ProductType.ProductTypeID == 2 ? entry.Purchase.Quantity : 0)
    }
    

    which gets translated into the following SQL:

    SELECT SUM(
        (CASE 
            WHEN [t2].[ProductTypeID] = @p0 THEN [t0].[Amount]
            ELSE @p1
         END)) AS [ClothingAmount], SUM(
        (CASE 
            WHEN [t2].[ProductTypeID] = @p2 THEN [t0].[Amount]
            ELSE @p3
         END)) AS [FoodAmount], SUM(
        (CASE 
            WHEN [t2].[ProductTypeID] = @p4 THEN [t0].[Quantity]
            ELSE @p5
         END)) AS [ClothingQuantity], SUM(
        (CASE 
            WHEN [t2].[ProductTypeID] = @p6 THEN [t0].[Quantity]
            ELSE @p7
         END)) AS [FoodQuantity], [t3].[StoreName]
    FROM [Purchase] AS [t0]
    INNER JOIN [Product] AS [t1] ON [t0].[ProductID] = [t1].[ProductID]
    INNER JOIN [ProductType] AS [t2] ON [t1].[ProductTypeID] = [t2].[ProductTypeID]
    INNER JOIN [Store] AS [t3] ON [t0].[StoreID] = [t3].[StoreID]
    GROUP BY [t3].[StoreName]
    

    Note however that it is highly recommended to set proper primary and foreign keys on the tables. Once that’s done (and the Model is updated accordingly) the same query can be reduced to:

    from purchase in Purchases
    group purchase by purchase.Store.StoreName into grouped
    select new {
        StoreName = grouped.Key,
        ClothingAmount = grouped.Sum(purchase => purchase.Product.ProductTypeID == 1 ? purchase.Amount : 0),
        FoodAmount = grouped.Sum(purchase => purchase.Product.ProductTypeID == 2 ? purchase.Amount : 0),
        ClothingQuantity = grouped.Sum(purchase => purchase.Product.ProductTypeID == 1 ? purchase.Quantity : 0),
        FoodQuantity = grouped.Sum(purchase => purchase.Product.ProductTypeID == 2 ? purchase.Quantity : 0)
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble getting this simple query to execute. I'm using nameless parametized queries
This is a weired problem, I have implemented simple quick sort as follows.. static
This is a simple bubble sort using a function pointer for ascending or descending.
I'm killing myself and dehydrating trying to get this array to sort. I have
I have this simple graph: name -> string ^ | v label let matrix
I have this simple array: var RedirUrl = new Array(4); RedirUrl[0] = 'http://mafi.se/mf_redir/new_install_'+this_version+'.html'; RedirUrl[1]
Given SQL Server 2008, I have written a simple find in string function as
Got this simple piece of html code and I want to make the TEST
Ill try to keep this simple and to the point. Essentially I have a
This is simple code in C (selection sort): #include <stdio.h> #include <stdlib.h> #define ItemCount

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.